﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Catalog;

namespace OnlineFloralShop
{
    public partial class Cart : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)        
         {
             if (!IsPostBack)
             {
                 //Show list item
                 ListView_Cart.DataSource = ((Dictionary<string, int>)Session["cart"]);
                 ListView_Cart.DataBind();

                 //Get link continue shopping
                 if (((Dictionary<string, int>)Session["cart"]).Count >0 && Session["AddToCartReferrer"] != null)
                 {

                         ((HyperLink)ListView_Cart.FindControl("hlinkContinue")).NavigateUrl = Session["AddToCartReferrer"].ToString();
                 }
             }
             //Refresh label check quantity
             Label_checkQuantity.Text = "";
            
        }
        
         //Event Click btnUpdate
         protected void btnUpdate_Click(object sender, EventArgs e)
         {
             UpdateCart();
         }


         //-------------------------------------------------------//
         //Method update this cart
         public void UpdateCart()
         {

             for (int i = 0; i < ListView_Cart.Items.Count; i++)
             {
                
                 int quantity = int.Parse(((TextBox)ListView_Cart.Items[i].FindControl("txtQuantity")).Text);

                 //Remove item if checked or quantity <=0
                 if (((CheckBox)ListView_Cart.Items[i].FindControl("chb_Remove")).Checked || quantity <=0)
                 {
                     ((Dictionary<string, int>)Session["cart"]).Remove(((Label)ListView_Cart.Items[i].FindControl("lbProId")).Text);

                 }
                 //Update Quantity
                 else
                 {
                         //Check so luong update co nhiu hon trong data khong
                         if (Common.CommonController.CheckQuantityAddToCart(((Label)ListView_Cart.Items[i].FindControl("lbProId")).Text, quantity))
                         {

                             //dua ra thong bao khong du hang`
                             Label_checkQuantity.Text += "* Item <b>" + ((Label)ListView_Cart.Items[i].FindControl("lbProId")).Text + "</b> buy more than quantity in Stock !<br />";

                         }
                         else
                         {
                             //Thuc hien update
                             ((Dictionary<string, int>)Session["cart"])[((Label)ListView_Cart.Items[i].FindControl("lbProId")).Text] = int.Parse(((TextBox)ListView_Cart.Items[i].FindControl("txtQuantity")).Text);
                            
                         }
                 }
             }
             //Refresh List
             ListView_Cart.DataSource = ((Dictionary<string, int>)Session["cart"]);
             ListView_Cart.DataBind();

         }

        //-------------------------------------------------------------------------------------//
        
        
       


        //Checkout

    }
}
